

require "palace.lua"

SnapState = {
  NORMAL = 0,
  LIUING = 1,
  LIUED = 2,
  BIXINING = 3,
  BIXINED = 4,
  SNAPSHOT = 5,
  VIDEO = 6,
}
g_state = 0
g_speed = 1.0 --缩放速度
isVideoing = false
totalElapsedTime = 0

function initialize(scene)

  kuruEngineInstance = KuruEngine.getInstance()
  scene:getInternalScene():setHandDetection(true)
  kuruBody = KuruHumanActionExtension.cast(kuruEngineInstance:getExtension("KuruHumanAction"))

  g_state = SnapState.NORMAL

  snapShot = getCurrentSnapshot(scene,1.0)
  snapShot2 = getCurrentSnapshot(scene,1.0)

  local g_framebuffer=  addNodeAndRelease(scene, KuruFrameBufferNode.create())
  local g_bgNode = addChildAndRelease(g_framebuffer, KuruBackgroundImageNode.createFromSampler(snapShot:getSampler(), BlendMode.None))

  local g_framebuffer2=  addNodeAndRelease(scene, KuruFrameBufferNode.create())
  local g_bgNode2 = addChildAndRelease(g_framebuffer2, KuruBackgroundImageNode.createFromSampler(snapShot2:getSampler(), BlendMode.None))

  g_place = PalaceEffect:init(scene,EffectType.effectType_2x1,false)
  g_place:add(g_framebuffer:getSampler(),1)
  g_place:add(g_framebuffer2:getSampler(),2)
  g_place:commit()
  
  sampler2 = getStickerSampler("b_qq",15)
  local w1 = 2.0
  local h1 = w1*9/16
  node2 = KuruFloatingImageNode.createFromSampler(sampler2,-1,-0.13,w1,h1,BlendMode.None)
  node2:setEnabled(false)
  addNodeAndRelease(scene,node2)
  sampler2:release()

  sampler3 = getStickerSampler("f_fff",15)
  sampler3:play()
  local w2 = 1.0
  local h2 = w2*9/16
  node3 = KuruFloatingImageNode.createFromSampler(sampler3,-1,-0.13,w2,h2,BlendMode.None)
  node3:setEnabled(false)
  addNodeAndRelease(scene,node3)
  sampler3:release()

  sampler4 = getStickerSampler("b_q",15)
  local w3 = 2
  local h3 = 2
  node4 = KuruFloatingImageNode.createFromSampler(sampler4,-1,-1,w3,h3,BlendMode.None)
  node4:setEnabled(false)
  addNodeAndRelease(scene,node4)
  sampler4:release()

  local oribgNode = addNodeAndRelease(scene, KuruFloatingImageNode.create(BASE_DIRECTORY .. "b_updown.png", -1, -1, 2, 2, BlendMode.None))

  local gesW1 = 0.4
  local gesH1 = gesW1*9/16
  g_gesNode1 = addNodeAndRelease(scene, KuruFloatingImageNode.create(BASE_DIRECTORY .. "hand/01.png", -1+0.06, 1-gesH1-0.25+0.05, gesW1, gesH1, BlendMode.None))

  local gesW2 = 0.4
  local gesH2 = gesW2*9/16
  g_gesNode2 = addNodeAndRelease(scene, KuruFloatingImageNode.create(BASE_DIRECTORY .. "hand/02.png", -1+0.04, 0-gesH2-0.08+0.05, gesW2, gesH2, BlendMode.None))
  
  soundExtension1 = KuruSoundExtension.cast(KuruEngine.getInstance():getExtension("KuruSound"))
  -- g_sound1 = soundExtension1:loadEx(BASE_DIRECTORY.."music.mp3")
  -- g_sound1:play(1,true)

  -- soundExtension2 = KuruSoundExtension.cast(KuruEngine.getInstance():getExtension("KuruSound"))
  g_sound2 = soundExtension1:loadEx(BASE_DIRECTORY..("one.mp3"))
  g_sound2:stop()

  -- soundExtension3 = KuruSoundExtension.cast(KuruEngine.getInstance():getExtension("KuruSound"))
  g_sound3 = soundExtension1:loadEx(BASE_DIRECTORY..("two.mp3"))
  g_sound3:stop()

end

function resetState()
          totalElapsedTime = 0.0
  
          node2:setEnabled(false)
          node3:setEnabled(false)
          node4:setEnabled(false)

          snapShot:setEnabled(true)
          snapShot2:setEnabled(true)

          g_gesNode1:setEnabled(true)
          g_gesNode2:setEnabled(true)
          -- g_sound1:stop()
          -- g_sound1:play(true)
end
function frameReady(scene, elapsedTime)
 
  if SceneRenderConfig.instance():isRenderModePreview() then
      if g_state == SnapState.SNAPSHOT or isVideoing then
          g_state = SnapState.NORMAL
          resetState()
          isVideoing = false
      end

  elseif  SceneRenderConfig.instance():isRenderModeSnapshot() then
        g_state = SnapState.SNAPSHOT
  elseif SceneRenderConfig.instance():isRenderModeVideo() then
       -- g_state = SnapState.SNAPSHOT
       if isVideoing==false then
          isVideoing = true
          resetState()
          g_state = SnapState.NORMAL
       end
        print("isRenderModeVideo")
  else

  end
  if g_state==SnapState.LIUING then
      if totalElapsedTime<0.5 then
          totalElapsedTime = totalElapsedTime+g_speed * elapsedTime * 0.001
      else
        sampler2:stop()
        sampler2:setRepeatCount(1)
        sampler2:setFPS(20)
        sampler2:play()

        node2:setEnabled(true)
        snapShot:setEnabled(false)
       
        totalElapsedTime=0.0

        g_state = SnapState.LIUED
      end 
  end
  if g_state==SnapState.BIXINING then
      if totalElapsedTime<0.5 then
          totalElapsedTime = totalElapsedTime+g_speed * elapsedTime * 0.001
      else 
        sampler3:stop()
        sampler3:setRepeatCount(1)
        sampler3:setFPS(20)
        sampler3:play()

        sampler4:stop()
        sampler4:setRepeatCount(1)
        sampler4:setFPS(20)
        sampler4:play()

        node3:setEnabled(true)
        node4:setEnabled(true)
        snapShot2:setEnabled(false)
        
        totalElapsedTime=0.0
        g_state = SnapState.BIXINED
      end 
  end
  local handCount = kuruBody:getHandCount()

  if handCount > 0 then
    for i = 0, handCount - 1 do
      local hand = kuruBody:getHandData(i)
      local vec2 = hand:getKeyPoint(0)
      local handRect = hand:getHandRect()

      --local center = hand:getHandCenter()
      -- local w2 = 1.3
      -- local h2 = w2*0.9313*9/16
      -- node3:setRect(handRect.x+(handRect.width-w2)/2,handRect.y+handRect.height/2*0.70,w2,h2)
     if vec2.x > -10000 then
      if g_state==SnapState.BIXINING then
       local w2 = 1.3
       local h2 = w2*0.9313*9/16
       node3:setRect(vec2.x-w2*0.5,vec2.y-h2*0.67,w2,h2)
      end

      if hand:isActive(HandDataActionType.DEVIL) then
          if g_state==SnapState.NORMAL then
              g_state=SnapState.LIUING
              g_gesNode1:setEnabled(false)
              g_sound2:play(false)
          end
         
      end
      if hand:isFingerHeart() then 
        if g_state==SnapState.LIUED then
          --print("比心")
          g_state=SnapState.BIXINING
          g_gesNode2:setEnabled(false)
          g_sound2:stop()
          g_sound3:play(false)
        end    
      end
     end
  end
  end
end



function addNodeAndRelease(scene, node)
  scene:addNode(node)
  node:release()
  return node
end

function addChildAndRelease(node,subNode)
  node:addChild(subNode)
  subNode:release()
  return node
end

function getCurrentSnapshot(scene, bufferScale)
  local node = KuruSnapshotNode.create()
  scene:addNode(node)
  node:release()
  node:setFrameBufferScale(bufferScale, bufferScale)
  return node
end
function getStickerSampler(filepath,fps)
  local sp = KuruAnimationSampler.createFromPath(BASE_DIRECTORY..filepath,false,false)
  sp:stop()
  -- sp:setRepeatCount(1)
  -- sp:setFPS(fps)
  -- sp:play()
  return sp
end
